﻿<!doctype html>
<html>
<!-- InstanceBegin template="/Templates/template1.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta charset="utf-8">
<!-- InstanceBeginEditable name="doctitle" -->
<title>岗位权限设置</title>
<!-- InstanceEndEditable -->
<script type="text/javascript" src="../js/common.js"></script>
<script type="text/javascript" src="../js/utils.js"></script>
<script type="text/javascript" src="../js/checkLogin.js"></script>
<script type="text/javascript" src="../js/find.js"></script>
<link href="../css/style.css" type="text/css" rel="stylesheet" />
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
<script type="text/javascript">
var _ownRoot ;
var _dutyRoot;
var _parentIdList = new Array();
var _dutyId;
$(function(){
	var id = getIdByName("id");
	
	getDutyById(id)
	getOwnDutyFunc();
	getDutyFunc(id);
	loadTable()
	_dutyId = id;
	$(".parentRoot").change(function(){
		if($(this).is(":checked")){
		var parentId= $(this).attr("parentId")
		$("#parentId_"+parentId).attr("checked","checked");
		}
	})
})
function getDutyById(id){
	var dt = _findInfo("../duty/find/duty/"+id, null)
	
	if(IsNotEmpty(dt)&&dt.state==0){
				$("#Name").val(getHtmlString(getHeadIndexValue(0, dt, "Name")))
				$("#ShopName").val(getHtmlString(getHeadIndexValue(0, dt, "ShopName")))
				$("#Code").val(getHtmlString(getHeadIndexValue(0, dt, "Code")))
	}
}

function getOwnDutyFunc(){//获得该登陆用户的所有权限
	var flag = getIdByName("flag");
	if(flag==1){
		
	var dt = _findInfo("../userFun/find/allMenu", null);
	}else{
		
	var dt = _findInfo("../userFun/find/getUserMenu", null);
	}

	if(dt.state==0){
		_ownRoot = dt
	}
}

function getDutyFunc(id){//获得该岗位的所有权限
	var dt=_findInfo("../dutyFun/find/funcByDutyId/"+id,null)
	if(dt.state==0){
		_dutyRoot = dt
	}
}

function loadTable(){
	$("#rootList tbody").empty()
	for ( var i = 0; i < _ownRoot.result.data.length; i++) {
		var parentId = getHtmlInt(getHeadIndexValue(i, _ownRoot, "parentId"));
		if(parentId==0){
			var id=getHtmlInt(getHeadIndexValue(i, _ownRoot, "funId"))
			var name=getHtmlString(getHeadIndexValue(i, _ownRoot, "funName"))
				loadTr(id,name)			
		}
	}
	
}

function loadTr(ide,name){
	var m = 0
	var childList = new Array();
	for ( var i = 0; i < _ownRoot.result.data.length; i++) {
		var parentId = getHtmlInt(getHeadIndexValue(i, _ownRoot, "parentId"));
		if(parentId==ide){
			var id=getHtmlInt(getHeadIndexValue(i, _ownRoot, "funId"))
			childList.push(id)		
			
		}
	}
	var n =0 
	
	for ( var i = 0; i < childList.length; i++) {
		if(i%3==0){
		if(i==0){
			var t="<tr><th id='parentR_"+ide+"'><label><input class='parentRoot' id='parentId_"+ide+"'   value='"+ide+"' type=\"checkbox\"";
			for ( var m = 0; m < _dutyRoot.result.data.length; m++) {
				var dutyRootid=getHtmlInt(getHeadIndexValue(m, _dutyRoot, "funId"))
					if(dutyRootid==ide){
					t+="checked='checked'";
				}
			}
			t+="><span>"+name+"</span></label></th></tr>"
			
			$("#rootList tbody").append(t)
		}else{
			
		$("#rootList tbody").append("<tr></tr>")
		}	
		n++;
		}
		for ( var j = 0; j < _ownRoot.result.data.length; j++) {
			var id=getHtmlInt(getHeadIndexValue(j, _ownRoot, "funId"))
			if(childList[i]==id){	
				var htm= "<td><label><input class='parentRoot' type=\"checkbox\" parentId ='"+ide+"' value='"+id+"'";
					for ( var m = 0; m < _dutyRoot.result.data.length; m++) {
						var dutyRootid=getHtmlInt(getHeadIndexValue(m, _dutyRoot, "funId"))
						if(id==dutyRootid){
							htm+="checked='checked'";
						}
					}
				htm+="><span>"+getHtmlString(getHeadIndexValue(j, _ownRoot, "funName"))+"</span></label></td>"
				$("#rootList tbody tr:last").append(htm)
			
				
			}
		}
	if(i==(childList.length-1)){
		var x= (i+1)%3
		if(x!=0){
			x=3-x
			for ( var h = 0; h < x; h++) {
				$("#rootList tbody tr:last").append("<td></td>")
			}
		}
	}	
	}
	
	$("#parentR_"+ide).attr("rowspan",n)
	
}

function saveInfo(){
	
	var parser2 = new Object();
	var alljson = null;
	$("input[type='checkbox']:checked").each(function(){
		var funcId = $(this).val()
		var head=new Array("dutyId","funId")
		 var data=new Array(_dutyId,funcId)
		if (IsNotEmpty(data)){
			addToParser(head, data, "add" , parser2);
		}
	})
	alljson =  JSON.stringify(parser2);
	var dt = _insertInfo('../dutyFun/insert/insertInfo', {'data':alljson,'dutyId':_dutyId})	
}



</script>
</head>

<body>
<!-- InstanceBeginEditable name="EditRegion3" -->
<div class="container" id="container">
  <div class="container_R fr">
    <div class="place"><a href="javaScript:history.go(-1)" onclick="returnURL()">岗位设置</a><span> &gt; <span>岗位权限 <a href="javaScript:history.go(-1)" class="goHome">返回</a> </span></span></div>
    <div class="member_search">
      <div class="knowPerson">
        <p class="knowAll">
          <label> <span>店铺名称：</span>
            <input type="text" class="memberInfo" id="ShopName" readonly>
          </label>
          <label> <span>岗位编号：</span>
            <input type="text" class="memberInfo" id="Code" readonly>
          </label>
          <label> <span>岗位名称：</span>
            <input type="text" class="memberInfo" readonly id="Name">
          </label>
        </p>
      </div>
    </div>
    <table width="100%" cellpadding="0" cellspacing="1" class="member_list power" border="0" id="rootList">
      <tbody>
       
     </tbody>
    </table>
    <div class="BottomBtn"><input type="button" onclick="saveInfo()" value="保存信息"></div>
  </div>
  <div class="fc"></div>
</div>
<!-- InstanceEndEditable --> 
<script type="text/javascript" src="../js/header.js"></script> 
<script type="text/javascript" src="../js/left.js"></script> 
<script type="text/javascript" src="../js/footer.js"></script> 
<script type="text/javascript">
addMargin($(".hideDiv"))
clearThis("absoluteTxt");
</script>
</body>
<!-- InstanceEnd -->
</html>
